from django_filters import *
from django_filters.rest_framework import DjangoFilterBackend, FilterSet
from rest_framework.filters import SearchFilter, OrderingFilter

from mysqldb.models import *


class ListDjangoFilterBackend(DjangoFilterBackend):
    """自己定义过滤器"""

    def get_schema_fields(self, view):
        if view.action not in ["list"]:
            return []
        return super().get_schema_fields(view)


class CallmeListFilterSet(FilterSet):
    """范围过滤器"""

    pid = NumberFilter(field_name="pid", help_text="场所ID")
    rtype = CharFilter(field_name="rtype", help_text="呼叫类型")
    source = CharFilter(field_name="source", help_text="机号")
    vipid = NumberFilter(field_name="vipid", help_text="会员ID")
    target = NumberFilter(field_name="target", help_text="员工ID")
    answer = NumberFilter(field_name="answer", help_text="应答ID")
    overtime = NumberFilter(field_name="overtime", help_text="超时状态")
    status = NumberFilter(field_name="status", help_text="订单状态")
    start = DateTimeFilter(
        field_name="current", method="filter_start", help_text="开始时间"
    )
    end = DateTimeFilter(field_name="current", method="filter_end", help_text="结束时间")

    class Meta:
        model = AllinpayCallmeList
        fields = [
            "pid",
            "rtype",
            "target",
            "overtime",
            "vipid",
            "source",
            "answer",
            "status",
            "start",
            "end",
        ]

    def filter_start(self, qs, name, value):
        lookup = "__".join([name, "gte"])
        return qs.filter(**{lookup: value})

    def filter_end(self, qs, name, value):
        lookup = "__".join([name, "lte"])
        return qs.filter(**{lookup: value})
